﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_RecvGoodsCompConP : DockContent
    {
        private DGBY.BLL.SHCompTab m_bllSHCompTab = new DGBY.BLL.SHCompTab();

        private DGBY.BLL.SHCompConPeoTab m_bllSHCompConPeoTab = new DGBY.BLL.SHCompConPeoTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectSHCompConPeoTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];

        public Frm_RecvGoodsCompConP()
        {
            InitializeComponent();
        }

        private void Frm_RecvGoodsCompConP_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllSHCompTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxChooseComp.DataSource = ds.Tables[0];
                this.comboBoxChooseComp.DisplayMember = "公司名字";
                this.comboBoxChooseComp.ValueMember = "SHCompTabID";
                this.comboBoxChooseComp.SelectedIndex = 0;
            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewCompPersonAndPhoneList.DataSource = null;
            m_ds = m_bllSHCompConPeoTab.GetAllConPList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompPersonAndPhoneList.DataSource = m_ds.Tables[0];
                dataGridViewCompPersonAndPhoneList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewCompPersonAndPhoneList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strSHCompConPeoTabID = this.dataGridViewCompPersonAndPhoneList.Rows[this.dataGridViewCompPersonAndPhoneList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectSHCompConPeoTabID = Convert.ToInt32(strSHCompConPeoTabID, 10);
            cellclickSHCompConPeoTab();
        }

        private void cellclickSHCompConPeoTab()
        {
            DGBY.Model.SHCompConPeoTab modelSHCompConPeoTab = m_bllSHCompConPeoTab.GetModel(m_nSelectSHCompConPeoTabID);
            if (modelSHCompConPeoTab != null)
            {
                if (modelSHCompConPeoTab.SHCompTabID != null)
                    this.comboBoxChooseComp.SelectedValue = modelSHCompConPeoTab.SHCompTabID;
                this.textBoxConPerson.Text = modelSHCompConPeoTab.SHCompConPeoName;
                this.textBoxConPhone.Text = modelSHCompConPeoTab.SHCompConPeoTel;
            }
            else
            {
                this.textBoxConPerson.Text = "";
                this.textBoxConPhone.Text = "";
            }
        }

        private void dataGridViewCompPersonAndPhoneList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompPersonAndPhoneList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.SHCompConPeoTab modelSHCompConPeoTab = new DGBY.Model.SHCompConPeoTab();
                modelSHCompConPeoTab.SHCompConPeoName = this.textBoxConPerson.Text;
                modelSHCompConPeoTab.SHCompConPeoTel = this.textBoxConPhone.Text;

                if (this.comboBoxChooseComp.SelectedValue != null)
                    modelSHCompConPeoTab.SHCompTabID = Convert.ToInt32(this.comboBoxChooseComp.SelectedValue.ToString(), 10);

                int nResult = m_bllSHCompConPeoTab.Add(modelSHCompConPeoTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", this.comboBoxChooseComp.Text + "(" + modelSHCompConPeoTab.SHCompConPeoName + ")");

                    m_nSelectSHCompConPeoTabID = m_bllSHCompConPeoTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllSHCompConPeoTab.GetModel(m_nSelectSHCompConPeoTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.SHCompConPeoTab modelSHCompConPeoTab = m_bllSHCompConPeoTab.GetModel(m_nSelectSHCompConPeoTabID);
               // modelSHCompConPeoTab.SHCompConPeoTabID = m_nSelectSHCompConPeoTabID;
                modelSHCompConPeoTab.SHCompConPeoName = this.textBoxConPerson.Text;
                modelSHCompConPeoTab.SHCompConPeoTel = this.textBoxConPhone.Text;
                if (this.comboBoxChooseComp.SelectedValue != null)
                    modelSHCompConPeoTab.SHCompTabID = Convert.ToInt32(this.comboBoxChooseComp.SelectedValue.ToString(), 10);


                m_bllSHCompConPeoTab.Update(modelSHCompConPeoTab);

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxChooseComp.Text + "(" + modelSHCompConPeoTab.SHCompConPeoName + ")");
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllSHCompConPeoTab.GetModel(m_nSelectSHCompConPeoTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllSHCompConPeoTab.Delete(m_nSelectSHCompConPeoTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickSHCompConPeoTab();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewCompPersonAndPhoneList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "RecvGoodsCompConP.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }

        private void comboBoxChooseComp_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string strCompID = this.comboBoxChooseComp.SelectedValue.ToString();
                dataGridViewCompPersonAndPhoneList.DataSource = null;
                m_ds = m_bllSHCompConPeoTab.GetConPListByCompID(strCompID);
                if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
                {
                    dataGridViewCompPersonAndPhoneList.DataSource = m_ds.Tables[0];
                    dataGridViewCompPersonAndPhoneList.Columns[0].Visible = false;
                }
            }
            catch { }
        }
    }
}